import { getUserFromCookie, browserType } from './../utils/auth'
import { ACTIVEINDEX } from '../store/mutation-types/left-menu/index'

export default function({store, redirect, req, isServer, route}) {
    //headers undefined
    if (isServer && !req) return;
    var loggedIE = false;
    if (process.BROWSER_BUILD) {
        loggedIE = browserType(window.navigator.userAgent);
    } else {
        loggedIE = browserType(req.headers['user-agent']);
    }
    if (loggedIE) {
        return redirect('/below-ie8');
    }

    var fromIndex = false;
    if (route.query.fromIndex) {
        fromIndex = true;
    }

    var routeName = route.name;


    var fullPath = route.fullPath;
    const loggedUser = isServer ? getUserFromCookie(req, 'token') : window.localStorage.getItem('token');
    switch (routeName) {
    //1-
    case 'customer':
        store.commit(ACTIVEINDEX, {
            activeIndex: '1-1'
        });
        break;
    case 'customer-add':
        store.commit(ACTIVEINDEX, {
            activeIndex: '1-2'
        });
        break;
    case 'customer-seas':
        store.commit(ACTIVEINDEX, {
            activeIndex: '1-3'
        });
        break;
    case 'customer-new-record':
        store.commit(ACTIVEINDEX, {
            activeIndex: '1-4'
        });
        break;
    case 'customer-missed-call':
        store.commit(ACTIVEINDEX, {
            activeIndex: '1-5'
        });
        break;
    case 'customer-brandlist':
        store.commit(ACTIVEINDEX, {
            activeIndex: '1-6'
        });
        break;
    case 'customer-edit':
        store.commit(ACTIVEINDEX, {
            activeIndex: '1-10'
        });
        break;
    case 'customer-detail':
        store.commit(ACTIVEINDEX, {
            activeIndex: '1-10'
        });
        break;
    case 'customer-contact':
        store.commit(ACTIVEINDEX, {
            activeIndex: '1-10'
        });
        break;

    //2-
    case 'report':
        store.commit(ACTIVEINDEX, {
            activeIndex: '2-1'
        });
        break;
    case 'report-owner':
        store.commit(ACTIVEINDEX, {
            activeIndex: '2-2'
        });
        break;
    case 'report-follow':
        store.commit(ACTIVEINDEX, {
            activeIndex: '2-3'
        });
        break;
    case 'report-destination':
        store.commit(ACTIVEINDEX, {
            activeIndex: '2-4'
        });
        break;
    case 'report-online':
        store.commit(ACTIVEINDEX, {
            activeIndex: '2-5'
        });
        break;

    //3-
    case 'calender':
        store.commit(ACTIVEINDEX, {
            activeIndex: '3-1'
        });
        break;

    //4-
    case 'power-setting':
        store.commit(ACTIVEINDEX, {
            activeIndex: '4-1'
        });
        break;
    case 'power-setting-module':
        store.commit(ACTIVEINDEX, {
            activeIndex: '4-2'
        });
        break;

    //5-
    case 'power':
        store.commit(ACTIVEINDEX, {
            activeIndex: '5-1'
        });
        break;
    case 'power-post-manage':
        store.commit(ACTIVEINDEX, {
            activeIndex: '5-2'
        });
        break;
    case 'power-user-control':
        store.commit(ACTIVEINDEX, {
            activeIndex: '5-3'
        });
        break;
    case 'power-role-management-add':
        store.commit(ACTIVEINDEX, {
            activeIndex: '5-10'
        });
        break;
    case 'power-user-control-name':
        store.commit(ACTIVEINDEX, {
            activeIndex: '5-10'
        });
        break;

    //6-
    case 'setting':
        store.commit(ACTIVEINDEX, {
            activeIndex: '6-1'
        });
        break;
    case 'setting-message':
        store.commit(ACTIVEINDEX, {
            activeIndex: '6-2'
        });
        break;
    case 'power-tel-management':
        store.commit(ACTIVEINDEX, {
            activeIndex: '6-3'
        });
        break;

    //7-
    case 'warning-warnDesk':
        store.commit(ACTIVEINDEX, {
            activeIndex: '7-1'
        });
        break;
    case 'warning-collectList-id':
        store.commit(ACTIVEINDEX, {
            activeIndex: '7-2'
        });
        break;
    case 'warning-detailList-id':
        store.commit(ACTIVEINDEX, {
            activeIndex: '7-3'
        });
        break;

    //8-
    case 'data-charts-lowRate':
        store.commit(ACTIVEINDEX, {
            activeIndex: '8-1'
        });
        break;
    case 'data-charts-fakeRate':
        store.commit(ACTIVEINDEX, {
            activeIndex: '8-2'
        });
        break;
    case 'data-charts-dealerRate':
        store.commit(ACTIVEINDEX, {
            activeIndex: '8-3'
        });
        break;

    //9-
    case 'complaintlist':
        store.commit(ACTIVEINDEX, {
            activeIndex: '9-1'
        });

        break;
    
    case 'complaintlist-auditfail':
        store.commit(ACTIVEINDEX, {
            activeIndex: '9-2'
        });
        break;
    //10-
    case 'brandadmin':
        store.commit(ACTIVEINDEX, {
            activeIndex: '10-1'
        });
        break;
	case 'brandadmin-brandDetail':
        store.commit(ACTIVEINDEX, {
            activeIndex: '99-1'
        });
        break;
	case 'brandadmin-openRights':
        store.commit(ACTIVEINDEX, {
            activeIndex: '99-1'
        });
        break;
	case 'brandadmin-distributionOperation':
        store.commit(ACTIVEINDEX, {
            activeIndex: '99-1'
        });
        break;
    case 'brandadmin-brandEdit':
        store.commit(ACTIVEINDEX, {
            activeIndex: '99-1'
        });
        break;
    //11-
    case 'keyWord':
        store.commit(ACTIVEINDEX, {
            activeIndex: '11-1'
        });
        break;
	case 'keyWord-add':
        store.commit(ACTIVEINDEX, {
            activeIndex: '99-1'
        });
        break;
	case 'keyWord-detail':
        store.commit(ACTIVEINDEX, {
            activeIndex: '99-1'
        });
        break;
    case 'keyWord-edit':
        store.commit(ACTIVEINDEX, {
            activeIndex: '99-1'
        });
        break;
    //12-
    case 'seller':
        store.commit(ACTIVEINDEX, {
            activeIndex: '12-1'
        });
        break;
	case 'seller-add':
        store.commit(ACTIVEINDEX, {
            activeIndex: '99-1'
        });
        break;
	case 'b2cAdmin':
        store.commit(ACTIVEINDEX, {
            activeIndex: '13-1'
        });
        break;
    case 'b2cAdmin-shopManage':
        store.commit(ACTIVEINDEX, {
            activeIndex: '13-2'
        })
        break;
    case 'b2cAdmin-messageLog':
        store.commit(ACTIVEINDEX, {
            activeIndex: '14-1'
        });
        break;
//  case 'b2cAdmin-trend':
//      store.commit(ACTIVEINDEX, {
//          activeIndex: '15-1'
//      });
//      break;
    case 'b2cAdmin-promotionLink':
        store.commit(ACTIVEINDEX, {
            activeIndex: '15-1'
        });
        break;
    case 'b2cAdmin-formMan':
        store.commit(ACTIVEINDEX, {
            activeIndex: '16-1'
        });
        break;
    case 'b2cAdmin-helpList':
        store.commit(ACTIVEINDEX, {
            activeIndex: '16-2'
        });
        break;
    case 'b2cAdmin-marketList':
        store.commit(ACTIVEINDEX, {
            activeIndex: '17-1'
        });
        break;
    case 'b2cAdmin-marketList-discountlist':
        store.commit(ACTIVEINDEX, {
            activeIndex: '17-2'
        });
        break;
    case 'b2cAdmin-marketList-couponlist':
        store.commit(ACTIVEINDEX, {
            activeIndex: '17-3'
        });
        break;
    case 'b2cAdmin-marketList-enteryGift':
        store.commit(ACTIVEINDEX, {
            activeIndex: '17-4'
        });
        break;
	case 'userStatistics-userProfile':
        store.commit(ACTIVEINDEX, {
            activeIndex: '18-1'
        });
        break;
    case 'inform':
        store.commit(ACTIVEINDEX, {
            activeIndex: '19-1'
        });
        break;
    case 'task':
        store.commit(ACTIVEINDEX, {
            activeIndex: '20-1'
        });
        break;
    case 'task-taskDetail':
        store.commit(ACTIVEINDEX, {
            activeIndex: '20-99'
        });
        break;
    case 'task-taskStatistics':
        store.commit(ACTIVEINDEX, {
            activeIndex: '20-99'
        });
        break;
    case 'promotionalLog':
        store.commit(ACTIVEINDEX, {
            activeIndex: '21-1'
        });
        break;
    case 'MonitorPlan':
        store.commit(ACTIVEINDEX, {
            activeIndex: '22-1'
        });
        break;
    //10-
    case 'product':
        store.commit(ACTIVEINDEX, {
            activeIndex: '101-1'
        });
        break;
    case 'product-category':
        store.commit(ACTIVEINDEX, {
            activeIndex: '101-2'
        });
        break;
    case 'orders':
        store.commit(ACTIVEINDEX, {
            activeIndex: '102-1'
        });
        break;
    }
	
    if (routeName == 'login') {
        if (loggedUser) {
            return redirect('/customer');
        }
    } else if (routeName == 'index') {
        if (!loggedUser) {
            return redirect('/login');
        } else {
            return redirect('/customer');
        }
    } else if (routeName == 'customer-detail') {
        if (fromIndex) {
            return fullPath;
        } else if (!loggedUser) {
            return redirect('/login');
        }
    } else if (routeName == 'customer-add') {
        if (fromIndex && loggedUser) {
            return fullPath;
        } else if (!loggedUser) {
            return redirect('/login');
        }
    } else {
        if (!loggedUser) {
            return redirect('/login');
        }
    }
}